﻿IF (EXISTS(SELECT id from sysobjects where id=object_id('usp_searchVoters')))
	DROP PROCEDURE [dbo].[usp_searchVoters]
GO

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

create procedure usp_searchVoters
@DistrictId as nvarchar(50),
@PanchayatId as nvarchar(50),
@WardId as nvarchar(50),
@Name as nvarchar(50),
@FatherName as nvarchar(50),
@VoterId as nvarchar(50)
as

declare @SQLMainWHERE		AS NVARCHAR(MAX)
declare @SQLMainSELECT		AS NVARCHAR(MAX)

set @SQLMainSELECT = 'select * from TBLVOTER where 1=1'

set @SQLMainWHERE = ''

select @SQLMainWHERE =@SQLMainWHERE + ' and VoterId=''' + @VoterId + '''' where @VoterId is not null
select @SQLMainWHERE = @SQLMainWHERE + ' and DistrictId=''' + @DistrictId  + '''' where @DistrictId  is not null
select @SQLMainWHERE = @SQLMainWHERE + ' and PanchayatId=''' + @PanchayatId  + '''' where @PanchayatId  is not null
select @SQLMainWHERE = @SQLMainWHERE + ' and WardId=''' + @WardId  + '''' where @WardId  is not null
select @SQLMainWHERE = @SQLMainWHERE + ' and Name like ''%' + @Name  + '%''' where @Name  is not null
select @SQLMainWHERE = @SQLMainWHERE + ' and FatherName like ''%' + @FatherName  + '%''' where @FatherName  is not null







declare @query as nvarchar(max)
set @query = @SQLMainSELECT + @SQLMainWHERE
print @query
EXEC (@query)

/*
Declare @DistrictId as nvarchar(50)
Declare  @PanchayatId as nvarchar(50)
Declare @WardId as nvarchar(50)
Declare @Name as nvarchar(50)
Declare @FatherName as nvarchar(50)
Declare @VoterId as nvarchar(50)



set @DistrictId = null
set  @PanchayatId = null
set @WardId = null
set @Name = 'karuna'
set @FatherName = 'muthu'
set @VoterId = 'v00097'

execute usp_searchVoters  @DistrictId,@PanchayatId,@WardId ,@Name ,
@FatherName,@VoterId

*/


